// pages/order/flow/flow.js
import util from '../../../utils/utils';
import {
  FlowHttp
} from '../../../api/flow.js'

const flowHttp = new FlowHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderNumber: 0, //订单号
    flowInfo: {},
    flowAry: [],
    flowAryLen: 0
  },

  formatData(res) {
    for (let i in res['logistics']) {
      let item = res['logistics'][i]
      item['date'] = util.formatTime('MM-dd', item['acceptTime']);
      item['time'] = util.formatTime('HH:mm', item['acceptTime']);
    }
    return res;
  },

  /**
   * 获取订单信息
   */
  httpGetFlow() {
    return flowHttp.getFlow({
      orderNumber: this.data.orderNumber
    }).then(res => {
      console.log(res);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();

      let info = this.formatData(res);
      this.setData({
        flowInfo: info,
        flowAry: res.logistics,
        flowAryLen: res.logistics.length
      });
      console.log(info);
    }).catch(err => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();

      util.msg(err.msg && err.msg != '' ? err.msg : '网络错误,请检查后重试!')
    })
  },

  /**
   * 页面载入完成
   */
  initPage: function(options) {
    console.log(options.hasOwnProperty('id'))
    if (options.hasOwnProperty('id')) {
      this.setData({
        orderNumber: options.id
      })
      this.initData()
    } else {
      util.msg("订单号错误")
    }
  },

  initData() {
    let self = this
    wx.showNavigationBarLoading();
    this.httpGetFlow();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initData()
  },

  onCopyClick: function(e) {
    wx.setClipboardData({
      data: this.data.flowInfo.deliverNo,
      success: function(res) {
        console.log(res.data)
      }
    });
  },

})